package cate.game.activity.lostdreamland;

import cate.common.table.activity.lostdreamland.row.LostDreamlandRewardRow;
import cate.common.table.d.GDFight;
import cate.game.activity.lostdreamland.msg.LostDreamlandChallengeResultResp;
import cate.game.play.proce.play.PlayPO;
import cate.game.play.vo.PlayReportVO;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.farm.AbstractFarmFTC;

import java.util.ArrayList;
import java.util.List;


public class LostDreamlandFTC extends AbstractFarmFTC<LostDreamlandActivity> {
    /**
     * 当前挑战关卡的模板
     */
    private final LostDreamlandRewardRow tpl;
    @Override
    protected LostDreamlandActivity po(Role role) {
        return role.getActivity().getEnableActivitySingle(funcId);
    }

    public LostDreamlandFTC(Role role, int funcId, LostDreamlandRewardRow tpl) {
        this.funcId = funcId;
        this.tpl = tpl;
        setup(role);
    }

    @Override
    public void handleResult(Role role, PlayPO playPO) {
        super.handleResult(role, playPO);
        boolean win = playPO.winner == GDFight.Team.A;

        LostDreamlandActivity lostDreamland = po(role);
        if (lostDreamland == null) {
            return;
        }
        LostDreamlandHandler handler = role.getGame().activityGlobal.getHandler(funcId);
        if (handler == null) {
            return;
        }
        //之前的战报
        List<PlayReportVO> reports = new ArrayList<>();
        LostDreamlandChallengeResultResp resultResp = new LostDreamlandChallengeResultResp();
        if (win) {
            List<PlayPO> plays = handler.tryUpdate(role, playPO, tpl.day, tpl.level);
            //奖励发放
            lostDreamland.provideReward(tpl.id);
            resultResp.reward =  new MixRes(tpl.freeReward);
            for (PlayPO p : plays) {
                reports.add(new PlayReportVO(p));
            }
            lostDreamland.noticeUpdate();
        } else {
            List<PlayPO> plays = handler.removePlay(role);
            for (PlayPO p : plays) {
                reports.add(new PlayReportVO(p));
            }
            reports.add(new PlayReportVO(playPO));
        }
        resultResp.funcId = funcId;
        resultResp.day = tpl.day;
        resultResp.level = tpl.id;
        resultResp.win = win;
        resultResp.reports = reports;
        role.sendNow(resultResp);
    }
}
